package com.duoduo.grpc.client.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.duoduo.grpc.client.service.HelloService;
import com.duoduo.grpc.lib.hello.HelloRequest;
import com.duoduo.grpc.lib.hello.HelloResponse;
import com.duoduo.grpc.lib.hello.HelloServiceGrpc;

import io.grpc.Channel;
import net.devh.boot.grpc.client.inject.GrpcClient;

@Service
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase implements HelloService {

    @GrpcClient("grpc_local_server")
    private Channel channel;

    @Override
    public Map getGreeting(String name) {
        HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);
        HelloResponse response = blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());
        Map map = new HashMap<>();
        map.put("code", response.getCode());
        map.put("msg", response.getMessage());
        return map;
    }
}